package com.zgm.ch09.jdbc.rdbmsOperation._5_1_1;

import java.sql.Types;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;

public class SqlUpdateDemo {

	private Insert insert;
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("_5_1_1-context.xml", SqlUpdateDemo.class);
		
		SqlUpdateDemo sqlUpdateDemo = new SqlUpdateDemo((DataSource) ac.getBean("dataSource2"));
		sqlUpdateDemo.runInsert();
	}
	
	private static class Insert extends SqlUpdate {
		private static final String SQL = "insert into t_customer (id, first_name, last_name, " +
				" last_login, comments) values (?, ?, ?, ?, ?)";
		
		Insert(DataSource dataSource) {
			super(dataSource, SQL);
			super.declareParameter(new SqlParameter(Types.INTEGER));
			super.declareParameter(new SqlParameter(Types.VARCHAR));
			super.declareParameter(new SqlParameter(Types.VARCHAR));
			super.declareParameter(new SqlParameter(Types.TIMESTAMP));
			super.declareParameter(new SqlParameter(Types.CLOB));
		}
	}
	
	SqlUpdateDemo(DataSource dataSource) {
		this.insert = new Insert(dataSource);
	}
	
	private void runInsert() {
		this.insert.update(new Object[]{9L, "7", "zgm", null, null});
		this.insert.update(new Object[]{10L, "8", "zh", null, null});
	}
	
	
}
